package top.ijiujiu.util;

//import org.apache.commons.codec.binary.Base64;

import javax.servlet.http.HttpServletRequest;
import java.util.Random;

/**
 * AppUtil
 *
 * @Author 神秘的黄瓜
 * @Version 1.0
 * @Date 2020-04-01 10:33
 */
public class AppUtil {

    public static final String PROXY_APPID = "";
    public static String PROXY_BUSINESS_ID = "54644405da064077d1c2e316";


    public static String getBaseUrl(HttpServletRequest request) {
        return getBaseUrl(request, true);
    }

    public static String getBaseUrl(HttpServletRequest request, boolean isNeedPort) {
        String path = request.getContextPath();
        String basePath = request.getScheme() + "://" + request.getServerName();
        if (isNeedPort) {
            basePath += ":" + request.getServerPort();
        }
        basePath += path + "/";
        return basePath;
    }

    public static boolean isWeiXin(HttpServletRequest request) {
        String agent = request.getHeader("User-Agent");
        if (agent.contains("MicroMessenger")) {
            return true;
        }
        return false;
    }


    public static String getRandom(int length, boolean isUp) {
        Random random = new Random();
        char[] arrays = new char[]{'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', '1', '2', '3', '4', '5', '6', '7', '8', '9', '0'};
        StringBuffer buffer = new StringBuffer();

        for (int i = 0; i < length; ++i) {
            int r = random.nextInt(arrays.length);
            Character rr = Character.valueOf(arrays[r]);
            if (isUp && random.nextBoolean()) {
                rr = Character.valueOf(Character.toUpperCase(rr.charValue()));
            }

            buffer.append(arrays[r]);
        }

        return buffer.toString();
    }

//    public static String serializerString(Object obj) throws IOException {
//        ByteArrayOutputStream bos = new ByteArrayOutputStream();
//        ObjectOutput out = null;
//        out = new ObjectOutputStream(bos);
//        out.writeObject(obj);
//        byte[] yourBytes = bos.toByteArray();
//        return  Base64.encodeBase64String(yourBytes);
//    }

   /* *//**
     * 根据SESSION获取用户信息
     * @param request
     * @return
     *//*
    public static UserConsumer getSessionUser(HttpServletRequest request) {
        HttpSession session = request.getSession();
        UserConsumer user=(UserConsumer)session.getAttribute(StaticDefine.SESSION_USER);
        return user;
    }*/
}
